/**
 * the entry of g-bios bottom half
 */

#include <autoconf.h>
#include <g-bios.h>
#include <arch/arm.h>

.global GBotHalfEntry

GBotHalfEntry:
	b    L1

	.word  GB_MAGIC_BH
	.word  (g_pGBiosEnd - GBotHalfEntry)

vIntTab:
	.word HandleUndef
	.word HandleSWI
	.word HandleIabort
	.word HandleDabort
	.word HandleIRQ
	.word HandleFiq

L1:
	msr   cpsr, #(ARM_MODE_IRQ | ARM_INT_MASK)
	ldr   sp,   =IRQ_STACK_BASE

	msr   cpsr, #(ARM_MODE_FIQ | ARM_INT_MASK)
	ldr   sp,   =FIQ_STACK_BASE

	msr   cpsr, #(ARM_MODE_SVC | ARM_INT_MASK)
	mov   sp,   #SVC_STACK_BASE

	ldr   r0,   =vIntTab
	mov   r1,   #40
	ldm   r0,   {r2, r3, r4, r5, r6, r7}
	stm   r1,   {r2, r3, r4, r5, r6, r7}

	mov   fp,   #0
	bl    main
0:
	b     0b

HandleUndef:

HandleSWI:
	stmfd sp!, {r0 - r12, lr}
	ldmfd sp!, {r0 - r12, pc}^

HandleIabort:
	sub   lr, lr, #4
	stmfd sp!, {r0 - r12, lr}
	ldmfd sp!, {r0 - r12, pc}^

HandleDabort:
	sub   lr, lr, #8
	stmfd sp!, {r0 - r12, lr}
	ldmfd sp!, {r0 - r12, pc}^

HandleIRQ:
	sub   lr, lr, #4
	stmfd sp!, {r0 - r12, lr}
	bl    ReadIrqNum
	bl    DoIRQ
	ldmfd sp!, {r0 - r12, pc}^

HandleFiq:
	sub   lr, lr, #4
	stmfd sp!, {r0 - r7, lr}
	// bl    DoFIQ
	ldmfd sp!, {r0 - r7, pc}^

